<template>
  <div class="register-background">
    <div style="display: flex;width:30%;margin-left: 35%;height: 60%">
      <div
          style="display:flex;flex: 1; align-items:center;justify-content:center; background-color: rgba(255, 255, 255, 0.2);">
        <el-form :model="user" style="width: 65%;" :rules="rules" ref="registerRef">
          <div style="font-size: 20px;font-weight: bold;color:#ffff;text-align: center;margin-bottom: 20px;">
            注&nbsp&nbsp&nbsp&nbsp册
          </div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="user.username" :maxlength="20" :minlength="8"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码" v-model="user.password" :maxlength="20" :minlength="8"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
              <el-input prefix-icon="el-icon-circle-check" placeholder="请确认密码" style="margin-right: 8px; "
                        v-model="user.confirmPass" :maxlength="20" :minlength="8"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <span>请选择角色：</span>
            <el-radio-group v-model="user.role">
              <el-radio label="用户"></el-radio>
              <el-radio label="管理员"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;" @click="register">注&nbsp;册</el-button>
          </el-form-item>
          <div style="display: flex;padding-top: 15px">
            <div>已经有账号？去<span style="color: blue;cursor: pointer" @click="$router.push('/login')">&nbsp;登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    //确认密码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback() //表示通过
      }
    };
    return {
      user: {
        username: '',
        password: '',
        confirmPass:''
      },
      rules: {
        username: [
          {required: true, message: '请输入8-20位账号', trigger: 'blur', min: 8, max: 20},
        ],
        password: [
          {required: true, message: '请输入8-20位密码', trigger: 'blur', min: 8, max: 20},
        ],
        confirmPass: [
          {validator: validatePassword, trigger: 'blur',min: 8, max: 20}
        ],
        role: [
          {required: true, message: '请选择角色', trigger: 'change'},
        ],
      }
    }
  },
  created() {
  },
  methods: {
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/register', this.user).then(res => {
            if(res.code === '200'){
              this.$router.push('/login')
              this.$message.success('注册成功')
            }else{
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.register-background {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('../assets/login2.jpg');
  /*background-size: cover;*/
  background-size: 100% 100%
}
/deep/ .el-radio__label{
  /* 更改radio的label样式 */
  color:darkslategrey;
}
/deep/ .el-radio__input.is-checked+.el-radio__label {
  /* 更改被选中的radio的label样式 */
  color: midnightblue;
}
</style>